<template>
  <div class="list" ref="wrapper">
    <div >
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{$store.getters.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item in hotCities" :key="item.id">
            <div class="button" @click="handleCityClick(item.name)">{{item.name}}</div>
          </div>
          
        </div>
      </div>
      <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list" v-for="innerItem in item" :key="innerItem.id">
          <div class="item border-bottom" @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
        </div>    
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import { mapMutations } from 'vuex'
export default {
  name: "CityList",
  props:{
      hotCities:{
          type:Array,
          default:[]
      },
      cities:{
           type:Object,
          default:{}
      },
      letter:{
          type: String,
          default:''
      }
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper);
  },
  watch: {
      letter () {
        if(this.letter){
            const element = this.$refs[this.letter][0]
            this.scroll.scrollToElement(element)
        }
      }
  },
  methods:{
    handleCityClick(city){
      // 这种写法是通过分发action来间接调用Mutations 
      // this.$store.dispatch('updataCity',city)
      // 这种方法是直接调用Mutations   下面这句话相当于this.$store.commit('UPDATE_CITY',city)
      this.UPDATE_CITY(city)
      this.$router.push('/')
    },
    ...mapMutations(['UPDATE_CITY']),
  }
};
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.border-topbottom {
  &:before {
    borde-color: #ccc;
  }

  &:after {
    borde-color: #ccc;
  }
}

.border-bottom {
  &:before {
    borde-color: #ccc;
  }
}

.list {
  overflow: hidden;
  position: absolute;
  top: 1.6rem
  left: 0;
  right: 0;
  bottom: 0;

  .title {
    text-align: left;
    line-height: 0.44rem;
    background: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }

  .button-list {
    overflow: hidden;
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;

    .button-wrapper {
      float: left;
      width: 33.33%;

      .button {
        margin: 0.1rem;
        padding: 0.1rem 0rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }

  .item-list {
    .item {
      text-align: left;
      line-height: 0.76rem;
      padding-left: 0.2rem;
    }
  }
}
</style>